'use strict';

import { AsyncStorage } from 'react-native';

class DeviceStorage {
  static clear() {
    return AsyncStorage.clear();
  }

  static get(key) {
    return AsyncStorage.getItem(key).then(value => JSON.parse(value));
  }

  static set(key, value) {
    return AsyncStorage.setItem(key, JSON.stringify(value));
  }

  static update(key, value) {
    return DeviceStorage.get(key).then((item) => {
      value = typeof value === 'string' ? value : Object.assign({}, item, value);
      return AsyncStorage.setItem(key, JSON.stringify(value));
    });
  }

  static remove(key) {
    return AsyncStorage.removeItem(key);
  }

  static multiGet(...keys) {
    return AsyncStorage.multiGet([...keys]).then((stores) => {
      let data = {};
      stores.map((result, i, store) => {
        data[store[i][0]] = JSON.parse(store[i][1]);
      });
      return data;
    });
  }

  static multiRemove(...keys) {
    return AsyncStorage.multiRemove([...keys]);
  }
}

export default DeviceStorage;